<template>
	<view>
		<view class="wrap">
			<u-search placeholder="请输入关键字" v-model="query.keyword" @search="searchKey" @custom="searchKey"></u-search>

			<!-- 			<view class="but-add">
				<u-button type="primary" text="新增商品模板"></u-button>
			</view> -->
			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom"
				v-if="skuList.length !== 0">
				<view class="page-box">
					<view class="order" v-for="(item, index) in skuList" :key="item.id">
						<view class="item">

							<view class="content">
								<view class="title u-line-2">{{item.skuCode}}</view>
								<view class="type">名称: {{ item.itemName }}</view>
								<view class="type">商品合计: {{ item.itemTotal }}</view>
								<view class="avgprice">平均成本价: {{ item.itemPriceAvg }}</view>
								<view class="maxprice">最高成本价: {{ item.itemPriceMax }}</view>
								<view class="minprice">最低成本价: {{ item.itemPriceMin }}</view>
								<view class="type">平均售价: {{ item.salesPriceAvg ? item.salesPriceAvg:"-" }}</view>
								<view class="type">最高售价: {{ item.salesPriceMax ? item.salesPriceMax:"-" }}</view>
								<view class="type">最低售价: {{ item.salesPriceMin ? item.salesPriceMin:"-" }}</view>
								<view class="type">平均利润: {{ item.profitAvg ? item.profitAvg:"-" }}</view>
								<view class="type">利润率: {{ item.profitPercent ? item.profitPercent:"-"}}</view>
							</view>
							<view class="right">
								<image :src="item.itemImg" mode="aspectFill"></image>
							</view>
						</view>
						<view class="bottom">
							<view class="evaluate btn" @click="showModal(item.itemName)">明细</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
				</view>
			</scroll-view>
			<scroll-view scroll-y style="height: 100%;width: 100%;" v-else>
				<view class="page-box">
					<view>
						<view class="centre">
							<view class="explain">
								您还没有相关商品
								<view class="tips">请更新商品记录后再次查看</view>
							</view>
						<!-- 	<view class="btn"><u-button type="primary" text="新增商品模板" @click="showModal"></u-button>
							</view> -->
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>

	<u-modal :show="show" title="最近6单交易记录" content="内容" @confirm="() => show = false" @cancel="show=false">
		<view class="charts-box">
			<qiun-data-charts type="line" :opts="opts" :chartData="chartData" :ontouch="true" />
		</view>
	</u-modal>

</template>



<script>
	import {
		skuList,
		skuPriceLastSix
	} from "@/api/work/history/history.js";

	export default {
		data() {
			return {
				chartData: {},
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 10, 0, 15],
					enableScroll: false,
					animation: false,
					update: true,
					duration: 0,
					legend: {},
					xAxis: {
						disableGrid: true,
						scrollShow: true,
						itemCount: 4
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						}
					}
				},
				nextPageFlag: true,
				query: {
					pageNum: 1,
					pageSize: 10,
					keyword: ''
				},
				show: false,
				skuList: [],
				skuName: '',
				totalCount: 0,
				current: 0,
				swiperCurrent: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore']
			};
		},
		onLoad() {
			this.getSkuList();
		},
		computed: {

		},
		methods: {
			showModal(itemName) {
				this.show = true;
				this.getServerData(itemName);
			},
			// 触底懒加载
			reachBottom() {
				console.info("触底----开始加在skuList")
				this.loadStatus.splice(this.current, 1, "loading")
				if (this.nextPageFlag) {
					setTimeout(() => {
						console.info("延迟1.1s")
						this.getSkuList(this.current);
					}, 1100);
				}

			},
			searchKey() {
				this.query.pageNum = 1
				this.skuList = []
				this.getSkuList()
			},
			// 页面数据
			getSkuList() {
				this.nextPageFlag = false;
				const pageTotal = this.totalCount / this.query.pageSize
				console.info('pageTotal:' + pageTotal)
				console.info('pageNum:' + this.query.pageNum)
				if (true) {
					setTimeout(() => {
						skuList(this.query).then(response => {
							this.skuList.push(...response.rows)
							this.totalCount = response.total
							//如果 rows.length<pageSize,说明没有下一页了
							if (response.rows.length < this.query.pageSize) {
								this.nextPageFlag = false;
							} else {
								this.query.pageNum++
								this.nextPageFlag = true;
							}
						})
					}, 500)
					this.loadStatus.splice(this.current, 1, "loadmore")
				} else {
					this.loadStatus.splice(this.current, 1, "noMore")
				}
			},
			getServerData(itemName) {
				let data = {
					itemName: itemName,
				}
				skuPriceLastSix(data).then(res => {
					this.chartData = res.data
				})
			},

		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.charts-box {
		width: 100%;
		height: 300px;
	}

	.but-add {
		width: 20%;
		margin-right: 20rpx;
		margin-left: 20rpx;
	}

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #2979ff;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #2979ff;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			flex-direction: row;
			margin: 20rpx 0 0;

			.right {
				margin-top: 100rpx;
				margin-right: 80rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				flex: 1;

				/* 占据剩余空间 */
				.title {
					font-size: 38rpx;
					line-height: 50rpx;
					margin-bottom: 10rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-tips-color;
				}
				.minprice {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-success;
				}
				.maxprice {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-warning;
				}
				.avgprice {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-primary;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 28rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>